package com.apex.oauth.security.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/4/8
 * Time: 20:49
 */
public class ICECookieUtil {


    /**
     * 将令牌添加到cookie
     *
     * @param response
     * @param domain   作用域
     * @param token    令牌
     */
    public static void addCookie(HttpServletResponse response, String domain, String token) {
        Cookie cookie = null;
        try {
            cookie = new Cookie("iPlanetDirectoryPro", URLEncoder.encode(token, "gb2312"));
            cookie.setPath("/");
            cookie.setDomain(domain);
            cookie.setMaxAge(60 * 60);
            response.addCookie(cookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void addWeiXinCookie(HttpServletResponse response, String domain, String token) {
        Cookie cookie = null;
        cookie = new Cookie("CookieWDirectoryPro", token);
        cookie.setPath("/");
        cookie.setDomain(domain);
        cookie.setMaxAge(60*60*24*365);
        response.addCookie(cookie);
    }

    /**
     * 删除令牌
     *
     * @param response
     * @param domain
     * @param token
     */
    public static void removeCookie(HttpServletResponse response, String domain, String token) {
        Cookie cookie = null;
        cookie = new Cookie("iPlanetDirectoryPro", null);
        cookie.setPath("/");
        cookie.setDomain(domain);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
